<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class ConfigController extends Controller
{
     public function index()
    {
         // 查询数据
        $data= DB::table('config')->first();
        return view('admin.config.index', ['title' => '网站配置','data' => $data]);
    }
    public function add(Request $request){

            // 去除token
            $data = $request -> except('_token');
            // 处理关键词
            if(empty($request -> keywords)){
                $data = $request -> except('keywords');
            }
            // 处理标题
            if(empty($request -> title)){
                $data = $request -> except('title');
            }
            // 处理描述
            if(empty($request -> discription)){
                $data = $request -> except('discription');
            }
            // 处理版权
            if(empty($request -> copyright)){
                $data = $request -> except('copyright');
            }
            //处理logo
            if($request -> hasFile('logo'))
            {
                if($request -> file('logo') -> isValid())
                {
                    $suffix = $request -> file('logo') -> 
                        getClientOriginalExtension();
                    $fileName = time().mt_rand(10000,99999).'.'.$suffix;
                    //移动文件
                    $move = $request -> file('logo') -> move('./uploads', $fileName);
                }
                    if($move)
                    {
                        $data['logo'] = $fileName;
                    }else
                    {
                        $data['logo'] = 'logo.jpg';
                    }
            }else
            {
                $data['logo'] = 'logo.jpg';
            }
                // var_dump($data);
                if(empty($r = DB::table('config')->first())){

                    $res = DB::table('config') -> insert($data);
                }else{
                    $res = DB::table('config') -> where('id',$r ->id)-> update($data);

                }
                // dd($res);
                if($res)
                {
                    return redirect('/admin/config/index');
                }else
                {
                    return back();
                }

    }
    // ajax处理网站开关
    public function ajax(Request $request){
        if(empty($r = DB::table('config')->first())){

            $res = DB::table('config') -> insert(['open' => '1']);
        }else{
            $res = DB::table('config') -> where('id',$r ->id)-> update(['open'=>($r ->open ? '0':'1')]);

        }
                // dd($res);
        if($res)
        {
            $data= DB::table('config')->first();
            if($data -> open == 1){
                return 1;
            }else{
                return 0;
            }
        }else
        {
            return false;
        }








        $data= DB::table('config')->first();
        if($data -> open == 1){
            $res = DB::table('config') -> first();
            return 1;
        }else{
            return 0;
        }
    }
}
